const connection = require('@/app/database')
const SQLSTM = require('@/utils/statement')
const snowflake = require('@/utils/snowflake')

class ArticleService {
  async createArticle(article, userId) {
    const id = snowflake.generate()
    const info = SQLSTM.insert('art_info', {
      id,
      userId,
      title: article.title,
      cover: article.cover,
      digest: article.digest,
      status: article.status,
    })
    const content = SQLSTM.insert('art_content', { infoId: id, content: article.content })
    await connection.execute(info)
    await connection.execute(content)
  }

  async selectArticle(query, id) {
    if (id == 1) {
      const statement = SQLSTM.select('art_info', {
        order: [['createTime', 'DESC']],
        limit: {
          pageNum: query.pageNum,
          pageSize: query.pageSize,
        },
      })
      const [list] = await connection.execute(statement)
      const [[{ total }]] = await connection.execute(
        SQLSTM.select('art_info', {
          attrs: [{ fun: 'count', attr: '*', as: 'total' }],
        })
      )
      return {
        list,
        total,
      }
    } else {
      // 权限问题暂未完善 --------------------------------
      const statement = ``
      const result = await connection.execute(statement)
      return result[0]
    }
  }

  async selectArticleDetail(infoId) {
    const infoStm = `SELECT * FROM art_info WHERE id = ?`
    const [ArtInfo] = await connection.execute(infoStm, [infoId])
    const contentStm = `SELECT * FROM art_content WHERE infoId = ?`
    const [ArtContent] = await connection.execute(contentStm, [infoId])
    return {
      ...ArtInfo[0],
      content: ArtContent[0].content,
    }
  }

  async updateArticle(data) {
    const info = {}
    for (const key of ['title', 'digest', 'cover', 'status']) {
      info[key] = data[key]
    }
    const infoStm = SQLSTM.update('art_info', {
      attrs: info,
      where: { id: data.id },
    })
    await connection.execute(infoStm)

    if (data.content) {
      const contentStm = SQLSTM.update('art_content', {
        attrs: { content: data.content },
        where: { id: data.id },
      })
      await connection.execute(contentStm)
    }
  }
  async selectArticleListWeb(query) {
    const statement = SQLSTM.select('art_info', {
      where: {
        status: '1',
      },
      order: [['createTime', 'DESC']],
      limit: {
        pageNum: query.pageNum,
        pageSize: query.pageSize,
      },
    })
    const [list] = await connection.execute(statement)
    const [[{ total }]] = await connection.execute(
      SQLSTM.select('art_info', {
        attrs: [{ fun: 'count', attr: '*', as: 'total' }],
        where: { status: '1' },
      })
    )
    return {
      list,
      total,
    }
  }
  async selectArticleDetailWeb(infoId) {
    const infoStm = `SELECT * FROM art_info WHERE id = ?`
    const [artInfo] = await connection.execute(infoStm, [infoId])
    const contentStm = `SELECT * FROM art_content WHERE infoId = ?`
    const [artContent] = await connection.execute(contentStm, [infoId])
    const { userId } = artInfo[0]
    const authorStm = `SELECT account, nickName, avatar, sex, remark FROM sys_user WHERE id = ?`
    const [author] = await connection.execute(authorStm, [userId])
    return {
      ...artInfo[0],
      content: artContent[0]?.content ?? '',
      author: author[0],
    }
  }
}

module.exports = new ArticleService()
